package top.jingles.sayhiintime;

import com.iflytek.tts.TtsService.Tts;

import java.io.IOException;
import java.io.RandomAccessFile;

public class TTSEngine {
    private static final TTSEngine sInstance = new TTSEngine();
    final int TTSRole = 1280;
    final int TTSSpeed = 1282;
    private boolean isNotStarted = true;
    private int isTtsState = 0;
    int rolesetting = -1;
    int speedsetting = -1;
    private String ttsContent;
    private String ttsPath = null;
    TtsThread ttsThread = new TtsThread();

    public static TTSEngine getInstance() {
        return sInstance;
    }

    void ClearListner() {
        this.ttsThread.listener = null;
    }

    public int GetRole() {
        return Tts.JniGetParam(TTSRole);
    }

    public int GetSpeed() {
        return Tts.JniGetParam(TTSSpeed);
    }

    public boolean IsNotPlay() {
        return this.ttsThread.IsNotPlay();
    }

    void LoadSetting() {
        try {
            RandomAccessFile file = new RandomAccessFile(this.ttsPath.substring(0, this.ttsPath.lastIndexOf("/") + 1) + "setting.ini", "rw");
            this.speedsetting = file.readInt();
            this.rolesetting = file.readInt();
        } catch (IOException ex) {
            System.out.println("load setting error: " + ex.getMessage());
        }
    }

    void SaveSetting() {
        try {
            RandomAccessFile file = new RandomAccessFile(this.ttsPath.substring(0, this.ttsPath.lastIndexOf("/") + 1) + "setting.ini", "rw");
            file.writeInt(Tts.JniGetParam(TTSSpeed));
            file.writeInt(Tts.JniGetParam(TTSRole));
        } catch (IOException ex) {
            System.out.println("save setting error: " + ex.getMessage());
        }
    }

    public void SetContent(String ttsContent) {
        this.ttsContent = ttsContent;
    }

    void SetListener(OnTtsThreadListener listener) {
        this.ttsThread.listener = listener;
    }

    public void SetRole(int role) {
        Tts.JniSetParam(TTSRole, role);
    }

    public void SetSpeed(int speed) {
        Tts.JniSetParam(TTSSpeed, speed);
    }

    public void SetTTSPath(String ttsPath) {
        this.ttsPath = ttsPath;
    }

    public void Start() {
        this.ttsThread = new TtsThread();
        SetListener(new OnTtsThreadListener() {
            public int OnCurrentTaskComplited() {
                TTSEngine.this.Stop();
                return 0;
            }
        });
        this.ttsThread.Play(this.ttsContent);
        LoadSetting();
        if (this.isNotStarted) {
            this.ttsThread.start();
            this.isNotStarted = false;
        }
        this.isTtsState = 1;
    }

    public void Stop() {
        if (this.isTtsState != 1) {
            return;
        }
        SaveSetting();
        Tts.JniStop();
        this.ttsThread.stop();
        this.isNotStarted = true;
        this.isTtsState = 0;
    }

    public class TtsThread
            extends Thread {
        public int currentPosition = 0;
        boolean isNotPlay = true;
        private OnTtsThreadListener listener = null;
        boolean notInitialized = true;
        private String ttsContent;

        TtsThread() {
        }

        public boolean IsNotPlay() {
            return this.isNotPlay;
        }

        public void Play(String words) {
            this.isNotPlay = false;
            this.ttsContent = words;
        }

        public void run() {
            if (this.notInitialized) {
                Tts.JniCreate(TTSEngine.this.ttsPath);
                if ((TTSEngine.this.rolesetting == -1) || (TTSEngine.this.speedsetting == -1)) {
                    Tts.JniSetParam(1, 1);
                } else {
                    Tts.JniSetParam(TTSRole, TTSEngine.this.rolesetting);
                    Tts.JniSetParam(TTSSpeed, TTSEngine.this.speedsetting);
                }
            }
            for (; ; ) {
                this.notInitialized = false;
                try {
                    sleep(100L);
                    if (!this.isNotPlay) {
                        Tts.JniSpeak(this.ttsContent);
                        if (this.listener != null) {
                            this.listener.OnCurrentTaskComplited();
                        }
                        this.isNotPlay = true;
                    }
                } catch (InterruptedException ex) {
                    System.out.println("tts jni speak error: " + ex.getMessage());
                }
            }
        }
    }
}
